﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
using Client.App.Models;
using Client.App.ViewModels.Interfaces;
using Core.Mvvm;
using Core.Mvvm.ViewModels;
using Multiprocessing.Core.Algorithm;

namespace Client.App.ViewModels.Implementations
{
    public class AlgorithmsViewModel : NotifiedObject, IAlgorithmsViewModel
    {
        #region Fields

        private ObservableCollection<AlgorithmFamily> _algorithmFamilies;
        private IAlgorithm _selectedAlgorithm;
        private AlgorithmFamily _selectedFamily;

        #endregion

        #region Events

        public event EventHandler<ParametrizedEventArgs<IAlgorithm>> ChangeAlgorithm;

        #endregion

        #region Constructors

        public AlgorithmsViewModel()
        {
            this.AlgorithmFamilies = new ObservableCollection<AlgorithmFamily>();
        }

        public AlgorithmsViewModel(IEnumerable<AlgorithmFamily> algorithmFamilies)
        {
            this.AlgorithmFamilies = new ObservableCollection<AlgorithmFamily>(algorithmFamilies);
        }

        #endregion

        #region Properties

        public AlgorithmFamily SelectedFamily
        {
            get { return _selectedFamily; }
            set { SetProperty(ref _selectedFamily, value, () => this.SelectedFamily); }
        }

        public ObservableCollection<AlgorithmFamily> AlgorithmFamilies
        {
            get { return _algorithmFamilies; }
            set { this.SetProperty(ref _algorithmFamilies, value, () => this.AlgorithmFamilies); }
        }

        public IAlgorithm SelectedAlgorithm
        {
            get { return _selectedAlgorithm; }
            set
            {
                SetProperty(ref _selectedAlgorithm, value, () => this.SelectedAlgorithm);
                OnChangeAlgorithm(value);
            }
        }

        #endregion

        #region Methods

        protected virtual void OnChangeAlgorithm(IAlgorithm algorithm)
        {
            EventHandler<ParametrizedEventArgs<IAlgorithm>> handler = Volatile.Read(ref this.ChangeAlgorithm);
            if (!ReferenceEquals(handler, null))
            {
                handler(this, new ParametrizedEventArgs<IAlgorithm>(algorithm));
            }
        }

        #endregion
    }
}
